<template>
  <FullLoading :full-loading="!tabbarItem" />
  <view
    class="puppet-taabar-page_container"
    :style="{
      backgroundColor: '#f5f5f5',
    }"
  >
    <template v-if="tabbarItem">
      <!-- <Member v-if="getItem.component === DynamicTabbarPagesEnum.BASE_MEMBER" /> -->
      <ContactsMap
        v-if="tabbarItem.component === DynamicTabbarPagesEnum.BASE_CONTACTSMAP"
        :is-tabbar-page="true"
      />
      <ActivityMain
        v-else-if="tabbarItem.component === DynamicTabbarPagesEnum.BASE_ACTIVITY"
        :is-tabbar-page="true"
      />
      <SupplyDemandSquareMain
        v-else-if="tabbarItem.component === DynamicTabbarPagesEnum.SUPPLYDEMANDSQUARE"
        :is-tabbar-page="true"
      />
      <template v-else>
        <Header :title="tabbarItem.text" :is-go-back="false" />
        <Empty text="Tabbar配置错误" />
      </template>
    </template>
    <TuiTabbar
      v-if="scene !== SceneValuesEnum.TIMELINE_SINGLEPAGE && tabbarItem"
      :current="current"
    />
  </view>
</template>

<script setup lang="ts">
  import TuiTabbar from '@/components/Tui-tabbar/Tui-tabbar.vue';
  import { useAppStore, type ITabbarItem } from '@/store/modules/app';
  import { propTypes } from '@/utils/propTypes';
  import { ref } from 'vue';
  // import Member from '../member/main.vue';
  import ActivityMain from '@/components/ActivityMain/index.vue';
  import SupplyDemandSquareMain from '@/components/SupplyDemandSquareMain/main.vue';
  import ContactsMap from '@/components/ContactsMap/main.vue';
  import { DynamicTabbarPagesEnum } from '@/enums/pageEnum';
  import Empty from '@/components/Empty/Empty.vue';
  import Header from '@/components/Header/index.vue';
  import { SceneValuesEnum } from '@/enums/sceneValuesEnum';
  import { onLoad, reLaunch, switchTab } from '@/router';
  import FullLoading from '@/components/FullLoading/index.vue';
  import { storeToRefs } from 'pinia';
  import { onShareAppMessage } from '@dcloudio/uni-app';
  import { useAppShare } from '@/hooks/core/useAppShare';

  const { scene } = uni.getEnterOptionsSync();
  const { setWxSharePath } = useAppShare();
  const useApp = useAppStore();
  const { tabbarList, globalConfig } = storeToRefs(useApp);

  const tabbarItem = ref<{ component: ITabbarItem['component']; text: ITabbarItem['text'] }>();

  const props = defineProps({
    current: propTypes.number,
  });

  onLoad<'puppetTaabarPage'>(
    async (op) => {
      const component = op?.component;
      const item = tabbarList.value[props.current];
      if (component) {
        if (scene === SceneValuesEnum.TIMELINE_SINGLEPAGE) {
          tabbarItem.value = { component: op.component, text: '' };
          return;
        }
        if (item?.component !== component) {
          const list = await useApp.tabbarApi();
          const tItem = list.find((v) => v.component === component);
          tItem ? switchTab(tItem.pagePath as any) : reLaunch({ name: component as any });
          return;
        }
      }

      tabbarItem.value = item;
    },
    () => {
      return {
        component: tabbarList.value[props.current].component,
      };
    },
  );

  onShareAppMessage(() => {
    const item = tabbarList.value[props.current];
    return {
      imageUrl: globalConfig.value?.comShareOptions.shareImg,
      path: setWxSharePath({
        name: `puppetTaabarPage_${props.current}` as any,
        options: { component: item.component },
      }),
      title: item.text,
    };
  });
</script>

<style lang="scss" scoped>
  .tmp-img {
    width: 100%;
    min-height: 100vh;
  }
</style>
